﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.WebApp.KM;
using ZX.DAL.DBExtend;
using System.Data;
using System.Data.Linq;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using ZX.BLL.Extend;
using ZX.KM.Core.Common;

namespace ZX.WebApp.DeskTop.Form
{
    public partial class DesktopConfigForAdmin : KmBasePage
    {
        private string _Action = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            _Action = QueryString("Action");
            switch (_Action)
            {
                case "LoadData":
                    LoadData();
                    break;
                case "Save":
                    Save();
                    break;
                case "Delete":
                    Delete();
                    break;
            }
        }

        protected void LoadData()
        {
            string roleID = QueryString("RoleID");
            //获取模块具体栏位布局信息
            //获取模块具体栏位布局信息
            DataSet ds = new DataSet();
            string strsql = @"select * from (select (case when srd.DeskTopTempID is null then 0 else 1 end) as IsChecked
                              ,srd.RoleDesktopID
	                          ,sdt.[DeskTopTempID]
                              ,sdt.[DeskTopTempName]
                              ,sdt.[Src]
                              ,ISNULL(srd.Height,sdt.[Height]) AS Height
                              ,sdt.[EntityName]
                              ,sdt.[Parameter]
                              ,ISNULL(srd.Sort,sdt.[Sort]) AS Sort
                              ,sdt.[PageType]
                              ,MIN(Coalesce(srd.PowerType,sdt.PowerType,0)) OVER (PARTITION  BY sdt.DeskTopTempID) AS PowerType
                              ,sdt.[TabTools]
                              ,sdt.[Region]
                              ,sdt.[More]
                              ,srd.RoleId
                        from SysDesktopTemplate sdt
                        LEFT JOIN SysRoleDesktop srd ON sdt.DeskTopTempID=srd.DeskTopTempID
                        and srd.RoleId='@RoleID') a Order by IsChecked desc,Sort asc";
            strsql = strsql.Replace("@RoleID", roleID);
            DbSql execsql = new DAL.DBExtend.DbSql();
            ds = execsql.Query(strsql);

            List<DeskTopModel> list = new List<DeskTopModel>();
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    DeskTopModel m = new DeskTopModel
                    {
                        id = row["DeskTopTempID"].ToString(),
                        title = row["DeskTopTempName"].ToString(),
                        pageType = row["PageType"].ToString(),
                        powerType = row["PowerType"].ToString(),
                        src = row["Src"].ToString(),
                        tools = row["TabTools"].ToString(),
                        more = row["More"].ToString(),
                        sort = row["Sort"].ToString(),
                        Region = row["Region"].ToString(),
                        IsChecked = row["IsChecked"].ToString(),
                        roleid = row["RoleId"].ToString(),
                        RoleDesktopID = row["RoleDesktopID"].ToString()
                    };
                    list.Add(m);
                }
            }
            ResponseJson(list);
        }

        protected void Save()
        {
            string Data = QueryString("Data");
            string roleID = QueryString("RoleID");

            List<DeskTopModel> listM = new List<DeskTopModel>();
            if (!string.IsNullOrEmpty(Data))
            {
                using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(Data)))
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(listM.GetType());
                    listM = (List<DeskTopModel>)serializer.ReadObject(ms);
                }
            }

            string xmlContent = Serializer<List<DeskTopModel>>.SerializeToXml(listM);
            string sql = "exec sp_SaveRoleDesktop '" + xmlContent + "','" + roleID + "'";
            DbSql execsql = new DAL.DBExtend.DbSql();
            execsql.ExecuteSql(sql);
            ResponseJson(true);
        }

        protected void Delete()
        {
            string DeskTopTempID = QueryString("DeskTopTempID");
            string sql = " delete from SysDesktopTemplate where DeskTopTempID='@DeskTopTempID' ";
            sql += " delete from SysRoleDesktop where DeskTopTempID='@DeskTopTempID' ";
            sql += " delete from SysUserDesktop  where RoleDesktopID='@DeskTopTempID' ";
            sql = sql.Replace("@DeskTopTempID", DeskTopTempID);
            sql = sql.Replace("@DeskTopTempID", DeskTopTempID);
            sql = sql.Replace("@DeskTopTempID", DeskTopTempID);

            DbSql execsql = new DAL.DBExtend.DbSql();
            execsql.ExecuteSql(sql);
            ResponseJson(true);
        }
    }
}